package com.cardvalue.sys.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;

import com.cardvalue.sys.R;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 切换步骤的gridview
 * Created by cardvalue on 2016/3/31.
 */
public class ScreenAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private Context context;
    private List<Map<String,Object>> list;
    private IsSelectedCallBack isSelectedCallBack;

    public ScreenAdapter(Context context,List<Map<String,Object>> list){
        this.inflater=LayoutInflater.from(context);
        this.context=context;
        this.list=list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Map<String,Object> getItem(int position) {
        return list==null?null:list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        HolderView holderView;
        if(convertView==null){
            holderView=new HolderView();
            convertView=inflater.inflate(R.layout.griditem_screen,null);
            holderView.rb_name=(RadioButton) convertView.findViewById(R.id.rb_name);
            convertView.setTag(holderView);
        }else{
            holderView=(HolderView)convertView.getTag();
        }
        holderView.rb_name.setText(list.get(position).get("name").toString());
        holderView.rb_name.setChecked((Boolean) list.get(position).get("isChecked"));
        holderView.rb_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isSelectedCallBack != null) {
                    isSelectedCallBack.isSelected(position);
                }
                for (int i = 0; i < list.size(); i++) {
                    if (i == position) {
                        list.get(i).put("isChecked", true);
                    } else {
                        list.get(i).put("isChecked", false);
                    }
                }
                ScreenAdapter.this.notifyDataSetChanged();
            }
        });
        return convertView;
    }

        public interface IsSelectedCallBack{
            void isSelected(int position);
        }
    public void setOnIsSelectedCallBack(IsSelectedCallBack isSelectedCallBack){
        this.isSelectedCallBack=isSelectedCallBack;
    }

    class  HolderView{
        RadioButton rb_name;//切换的第几步
    }

}
